/***
    Amrita ITEWS - Copyright (C) 2009 Amrita Vishwa Vidyapeetham, Amritapuri.
                                       (http://www.amrita.edu)
    ***************************************************************************
    This file is part of the Amrita ITEWS distribution.
    Amrita ITEWS is free software; you can redistribute it and/or modify it 
    under the terms of the GNU General Public License (version 2) as published
    by the Free Software Foundation AND MODIFIED BY the Amrita ITEWS exception.
    ***NOTE*** The exception to the GPL is included to allow you to distribute
    a combined work that includes Amrita ITEWS without being obliged to provide
    the source code for proprietary components outside of the Amrita ITEWS
    software. Amrita ITEWS is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
    Public License for more details. You should have received a copy of the GNU
    General Public License and the Amrita ITEWS license exception along with 
    Amrita ITEWS if not then it can be viewed here: 
    http://itews.amrita.ac.in/license.html.

    Documentation, latest information, license and contact details are at:
    http://itews.amrita.ac.in/

    Amrita ITEWS source code can be found at:
    http://code.google.com/p/itews

    The initial code-base of Amrita ITEWS was developed by Amrita Vishwa 
    Vidyapeetham as part of the project titled,"Intelligent & Interactive 
    Telematics using Emerging Wireless Technologies for Transport Systems 
    (ITEWS)" sponsored by Technology Information Forecasting and Assessment 
    Council (TIFAC), India.
***/

package baseplatformservices;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Properties;
import java.util.StringTokenizer;

public final class BasePlatformConfig
{
	public static final String GVIA_COMMON_CONFIG_FILE = "/etc/gvia/gviaConfig.properties";
	private static final String BASEPLATFORM_CONFIG_FILE = "baseplatformservicesConfig.properties";
	private int httpConnTimeoutSecs;
	private int httpSocTimeoutSecs;
	private String hotzoneInfoUrl;
	private int hotzoneInfoSubscribingInterval;
	private String feedURL;
	private String[] blogSubscriberApps;
	private int blogSubscribeIntervalSecs;
	private String jdbcConnectionURI;
	private String[] blogPublisherApps;
	private String eventTopicToBPSBundle;
	private int mediaLogExpiryTime;
	protected static BasePlatformConfig singleton = null;
	private Properties gviaCommonProps;
	private Properties configProperties;
	private String user;
	private String password;
	private int blogPublishIntervalSecs;
	private String telematicsUnit;
	
	private BasePlatformConfig() throws Exception
	{
		InputStream gviaIs = null;
		InputStream is = null;
		try
		{
			configProperties = new Properties();
			is = this.getClass().getResourceAsStream(BASEPLATFORM_CONFIG_FILE);
			if (is == null)
				throw new Exception(BASEPLATFORM_CONFIG_FILE + " file not found");
			configProperties.load(is);
			is.close();
			gviaCommonProps = new Properties();
			gviaIs = new FileInputStream(GVIA_COMMON_CONFIG_FILE);
			gviaCommonProps.load(gviaIs);
			gviaIs.close();
			setTelematicsUnit();
			setPassword();
			setUser();
			setHttpConnTimeoutSecs();
			setHttpSocTimeoutSecs();
			//setHotzoneInfoUrl();
			//setHotzoneInfoSubscribingInterval();
			setFeedURL();
			setBlogSubscriberApps();
			setBlogSubscribeIntervalSecs();
			setBlogPublishIntervalSecs();
			setBlogPublisherApps();
			setJdbcConnectionURI();
			setEventTopicToBPSBundle();
			setMediaLogExpiryTime();
		}
		catch(Exception e)
		{
			if (is != null)
				is.close();
			if (gviaIs != null)
				gviaIs.close();
			throw e;
		}
	}

	public static BasePlatformConfig getConfig() throws Exception 
	{
		try
		{
			if (singleton == null)
			{
				singleton = new BasePlatformConfig();
			}
		}
		catch(Exception e)
		{
			throw e;
		}
		return singleton;
	}
	
	public String getPassword() 
	{
		return password;
	}

	public void setPassword() throws Exception
	{
		String propValue = gviaCommonProps.getProperty("password");
		if (propValue == null)
			throw new Exception("password not added in " + GVIA_COMMON_CONFIG_FILE + " file");
		else
			password = propValue;
	}
	
	public String getTelematicsUnit() 
	{
		return telematicsUnit;
	}

	public void setTelematicsUnit() throws Exception
	{
		String propValue = gviaCommonProps.getProperty("telematicsUnit");
		if (propValue == null)
			throw new Exception("telematicsUnit not added in " + GVIA_COMMON_CONFIG_FILE + " file");
		else
			telematicsUnit = propValue;
	}
	
	public String getUser() 
	{
		return user;
	}

	public void setUser() throws Exception
	{
		String propValue = gviaCommonProps.getProperty("user");
		if (propValue == null)
			throw new Exception("user not added in " + GVIA_COMMON_CONFIG_FILE + " file");
		else
			user = propValue;
	}

	private void setHttpConnTimeoutSecs() throws Exception
	{
		String propValue = gviaCommonProps.getProperty("httpConnTimeoutSecs");
		if (propValue == null)
			throw new Exception("httpConnTimeoutSecs not added in " + GVIA_COMMON_CONFIG_FILE + " file");
		else
			httpConnTimeoutSecs = Integer.parseInt(propValue);
	}
	
	public int getHttpConnTimeoutSecs() 
	{
		return httpConnTimeoutSecs;
	}
	
	private void setHttpSocTimeoutSecs() throws Exception
	{
		String propValue = gviaCommonProps.getProperty("httpSocTimeoutSecs");
		if (propValue == null)
			throw new Exception("httpSocTimeoutSecs not added in " + GVIA_COMMON_CONFIG_FILE + " file");
		else
			httpSocTimeoutSecs = Integer.parseInt(propValue);
	}
	
	public int getHttpSocTimeoutSecs() 
	{
		return httpSocTimeoutSecs;
	}
	
	public void setHotzoneInfoUrl() throws Exception
	{
		String propValue = gviaCommonProps.getProperty("hotzoneInfoUrl");
		if (propValue == null)
			throw new Exception("hotzoneInfoUrl not added in " + GVIA_COMMON_CONFIG_FILE + " file");
		else
			hotzoneInfoUrl = propValue;
	}
	
	public String getHotzoneInfoUrl() 
	{
		return hotzoneInfoUrl;
	}
	
	public void setHotzoneInfoSubscribingInterval() throws Exception
	{
		String propValue = gviaCommonProps.getProperty("hotzoneInfoSubscribingInterval");
		if (propValue == null)
			throw new Exception("hotzoneInfoSubscribingInterval not added in " + GVIA_COMMON_CONFIG_FILE + " file");
		else
			hotzoneInfoSubscribingInterval = Integer.parseInt(propValue);
	}
	
	public int getHotzoneInfoSubscribingInterval() 
	{
		return hotzoneInfoSubscribingInterval;
	}
	
	private void setFeedURL() throws Exception
	{
		String propValue = gviaCommonProps.getProperty("feedURL");
		if (propValue == null)
			throw new Exception("feedURL not added in " + GVIA_COMMON_CONFIG_FILE + " file");
		else
			feedURL = propValue;
	}

	public String getFeedURL()
	{
		return feedURL;
	}
	
	private void setBlogSubscribeIntervalSecs() throws Exception
	{
		String propValue = gviaCommonProps.getProperty("blogSubscribeIntervalSecs");
		if (propValue == null)
			throw new Exception("blogSubscribeIntervalSecs not added in " + GVIA_COMMON_CONFIG_FILE + " file");
		else
			blogSubscribeIntervalSecs = Integer.parseInt(propValue);
	}
	
	public int getBlogSubscribeIntervalSecs() 
	{
		return blogSubscribeIntervalSecs;
	}
	
	private void setBlogPublishIntervalSecs() throws Exception
	{
		String propValue = gviaCommonProps.getProperty("blogPublishIntervalSecs");
		if (propValue == null)
			throw new Exception("blogPublishIntervalSecs not added in " + GVIA_COMMON_CONFIG_FILE + " file");
		else
			blogPublishIntervalSecs = Integer.parseInt(propValue);
	}
	
	public int getBlogPublishIntervalSecs() 
	{
		return blogPublishIntervalSecs;
	}
	
	private void setBlogSubscriberApps() throws Exception
	{
		String propValue = gviaCommonProps.getProperty("blogSubscriberApps");
		if (propValue == null)
		{
			throw new Exception("blogSubscriberApps not added in " + GVIA_COMMON_CONFIG_FILE + " file");
		}
		else
		{
			StringTokenizer st = new StringTokenizer(propValue, ",");
			ArrayList list = new ArrayList();
			while (st.hasMoreTokens())
			{
				list.add(st.nextToken());
			}
			blogSubscriberApps = new String [list.size()];
			list.toArray (blogSubscriberApps);
		}
	}
	
	public String[] getBlogSubscriberApps()
	{
		return blogSubscriberApps;
	}
	
	private void setBlogPublisherApps() throws Exception
	{
		String propValue = gviaCommonProps.getProperty("blogPublisherApps");
		if (propValue == null)
		{
			throw new Exception("blogPublisherApps not added in " + GVIA_COMMON_CONFIG_FILE + " file");
		}
		else
		{
			StringTokenizer st = new StringTokenizer(propValue, ",");
			ArrayList list = new ArrayList();
			while (st.hasMoreTokens())
			{
				list.add(st.nextToken());
			}
			blogPublisherApps = new String [list.size()];
			list.toArray(blogPublisherApps);
		}
	}
	
	public String[] getBlogPublisherApps()
	{
		return blogPublisherApps;
	}
	
	private void setMediaLogExpiryTime() throws Exception
	{
		String propValue = gviaCommonProps.getProperty("mediaLogExpiryTime");
		if (propValue == null)
			throw new Exception("mediaLogExpiryTime not added in " + GVIA_COMMON_CONFIG_FILE + " file");
		else
			mediaLogExpiryTime = Integer.parseInt(propValue);
	}
	
	public int getMediaLogExpiryTime() 
	{
		return mediaLogExpiryTime;
	}
	
	private void setJdbcConnectionURI() throws Exception
	{
		String propValue = configProperties.getProperty("jdbcConnectionURI");
		if (propValue == null)
			throw new Exception("jdbcConnectionURI not added in " + BASEPLATFORM_CONFIG_FILE + " file");
		else
			jdbcConnectionURI = propValue;
	}
	
	public String getJdbcConnectionURI()
	{
		return jdbcConnectionURI;
	}
	
	public void setEventTopicToBPSBundle() throws Exception
	{
		String propValue = configProperties.getProperty("eventTopicToBPSBundle");
		if (propValue == null)
			throw new Exception("eventTopicToBPSBundle not added in " + BASEPLATFORM_CONFIG_FILE + " file");
		else
			eventTopicToBPSBundle = propValue;
	}
	
	public String getEventTopicToBPSBundle() 
	{
		return eventTopicToBPSBundle;
	}
}
